package org.msfj.admin.data.service.impl;

import org.msfj.admin.data.service.FormFieldCategoryService;
import org.msfj.admin.model.FormFieldCategory;
import org.msfj.admin.vo.FormFieldCategoryVo;
import org.msfj.admin.data.service.FormFieldCategoryService;
import org.msfj.admin.model.FormFieldCategory;
import org.msfj.admin.vo.FormFieldCategoryVo;
import org.msfj.admin.data.mapper.FormFieldCategoryMapper;
import org.msfj.admin.data.service.FormFieldCategoryService;
import org.msfj.admin.model.FormFieldCategory;
import org.msfj.admin.vo.FormFieldCategoryVo;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "formFieldCategory")
public class FormFieldCategoryServiceImpl extends BaseServiceImpl<FormFieldCategory> implements FormFieldCategoryService {

    @Autowired
    FormFieldCategoryMapper formFieldCategoryMapper;

    @Override
    public FormFieldCategoryVo setOtherProperties(FormFieldCategory formFieldCategory) {
        FormFieldCategoryVo formFieldCategoryVo = new FormFieldCategoryVo();
        BeanUtils.copyProperties(formFieldCategory, formFieldCategoryVo);
        return formFieldCategoryVo;
    }

    @Override
    public List<FormFieldCategoryVo> setOtherProperties(List<FormFieldCategory> formFieldCategories) {
        List<FormFieldCategoryVo> formFieldCategoryVos = new ArrayList<>();


        if (formFieldCategories != null && !formFieldCategories.isEmpty()) {
            for (FormFieldCategory formFieldCategory : formFieldCategories) {
                FormFieldCategoryVo formFieldCategoryVo = new FormFieldCategoryVo();
                BeanUtils.copyProperties(formFieldCategory, formFieldCategoryVo);
                formFieldCategoryVos.add(formFieldCategoryVo);
            }
        }
        return formFieldCategoryVos;
    }

    @Override
    public List<FormFieldCategoryVo> getTree() {
        List<FormFieldCategory> formFieldCategories = formFieldCategoryMapper.selectAll();
        List<FormFieldCategoryVo> tree = setOtherProperties(formFieldCategories);
        return tree;
    }
}
